// components/position-toast/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    openPosition(){

      wx.getLocation({
        type:"wgs84",
        success:(res)=>{
          this.setData({
            currentLon: res.longitude,
            currentLat: res.latitude,
          });
         wx.setStorageSync('currentLon', this.data.currentLon)
         wx.setStorageSync('currentLat', this.data.currentLat)
        },
        fail() {
          wx.getSetting({
            success:(res) =>{
              var statu = res.authSetting;
              console.log(statu);
              if (!statu['scope.userLocation']) {
                wx.showModal({
                  title: '是否授权当前位置',
                  content: '需要获取您的地理位置，请确认授权，否则定位功能将无法使用',
                  success:(tip) => {
                    console.log(tip);
                    if (tip.confirm) {
                      wx.openSetting({
                        success: function (data) {
                          if (data.authSetting["scope.userLocation"] === true) {
                            wx.showToast({
                              title: '授权成功',
                              icon: 'success',
                              duration: 1000
                            })
                            wx.getLocation({
                              success(res) {
                                this.setData({
                                  currentLon: res.longitude,
                                  currentLat: res.latitude,
                                });
                                wx.setStorageSync('currentLon', this.data.currentLon)
                                wx.setStorageSync('currentLat', this.data.currentLat)
                              },
                            });
                          } else {
                            wx.showToast({
                              title: '授权失败',
                              icon: 'error',
                              duration: 1000
                            })
                            wx.navigateBack({
                              delta: -1
                            });
                          }
                        }
                      })
                    }else{
                      wx.navigateBack({
                        delta: -1
                      });
                    }
                  }
                })
              }
            },
            fail: function (res) {
              wx.showToast({
                title: '调用授权窗口失败',
                icon: 'success',
                duration: 1000
              })
              wx.navigateBack({
                delta: -1
              });
            }
          })
        }
      
      })
      
    },

  }
})
